package com.xiaozhengkeji.mc.xzlib.module;

import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.concurrent.Executors;

import static java.util.concurrent.Executors.*;


public class HttpModule {
    private HttpServer httpServer = null;

    public HttpModule(int httpServerPort, HashMap<String, HttpHandler> HttpContext, int NumberOfThreads) throws IOException {
        this.httpServer = HttpServer.create(new InetSocketAddress(httpServerPort), 0);
        //遍历并且创建server处理工厂
        for (String url : HttpContext.keySet()) {
            this.httpServer.createContext(url, HttpContext.get(url));
        }
        //设置服务器的线程池对象
        this.httpServer.setExecutor(newFixedThreadPool(NumberOfThreads));
        //启动服务器
        httpServer.start();
    }

}
